package com.csu.ourpetstore1.controller;

import com.csu.ourpetstore1.domain.Account;
import com.csu.ourpetstore1.domain.CartItem;
import com.csu.ourpetstore1.domain.Order;
import com.csu.ourpetstore1.service.AccountService;
import com.csu.ourpetstore1.service.CartService;
import com.csu.ourpetstore1.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    AccountService accountService;
    @Autowired
    CartService cartService;
    @Autowired
    OrderService orderService;
    //跳往结账界面
    @GetMapping("/checkout")
    public String GoToCheckout(@SessionAttribute("username") String username, Model model){
        Account account = accountService.getAccount(username);
        model.addAttribute("account",account);
        return "order/newOrderForm";
    }
    //查看订单详情
    @PostMapping("/viewdetailorder")
    public String ViewDetailOrder(Model model, String orderId){
        Order now_order = orderService.getOrder(Integer.parseInt(orderId));
        model.addAttribute("now_order",now_order);
        return "order/viewOrder";
    }
    @GetMapping("/viewdetailorder")
    public String ViewDetailOrder1(Model model, String orderId){
        Order now_order = orderService.getOrder(Integer.parseInt(orderId));
        model.addAttribute("now_order",now_order);
        return "order/viewOrder";
    }
    //查看我的订单
    @GetMapping("/viewMyOrder")
    public String ViewMyOrder(String username,Model model){
        List<Order> orderList=orderService.getOrdersByUsername(username);
        model.addAttribute("orderList",orderList);
        return "order/listOrders";
    }
    //跳往确认界面
    @PostMapping("/gotoconfirm")
    public String GoToConfirm(@SessionAttribute String username,@ModelAttribute Order order, Model model){
        order.setCourier("ZN");
        order.setStatus("P");
        order.setUsername(username);
        order.setOrderDate(new Date());
        orderService.insertOrder(order);
        model.addAttribute("now_order",order);
        return "order/confirmOrder";
    }


}
